﻿Namespace control
    ''' <summary>
    ''' 永胜托盘控件 2011/04/14
    ''' </summary>
    ''' <remarks></remarks>
    Public Class wysNotifyIcon

#Region "变量"
        Private _NotifyIcon As NotifyIcon
#End Region

#Region "属性"

        Private _主窗体 As System.Windows.Forms.Form = Nothing
        Public Property 主窗体() As System.Windows.Forms.Form
            Get
                Return _主窗体
            End Get
            Set(ByVal value As System.Windows.Forms.Form)
                _主窗体 = value
                If value IsNot Nothing Then
                    AddHandler _主窗体.Load, AddressOf 主窗体_Load
                Else

                End If
            End Set
        End Property

        Private _icon As Icon = My.Resources.托盘图标
        Public Property 托盘图片() As Icon
            Get
                Return _icon
            End Get
            Set(ByVal value As Icon)
                _icon = value
            End Set
        End Property

        Private _contextMenu As New ContextMenuStrip
        Public Property 托盘右键菜单() As ContextMenuStrip
            Get
                Return _contextMenu
            End Get
            Set(ByVal value As ContextMenuStrip)
                _contextMenu = value
            End Set
        End Property

        ''' <summary>
        ''' 做控件的初始化
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub 主窗体_Load(ByVal sender As Object, ByVal e As EventArgs)
            If _主窗体 IsNot Nothing Then
                _NotifyIcon = New NotifyIcon
                If 托盘图片 IsNot Nothing Then
                    _NotifyIcon.Icon = 托盘图片
                End If
                _NotifyIcon.Text = 主窗体.Text
                If 托盘右键菜单 IsNot Nothing Then
                    _NotifyIcon.ContextMenuStrip = 托盘右键菜单
                End If

                If 关闭窗体最小化托盘 Then
                    AddHandler 主窗体.FormClosing, AddressOf MainSecond_FormClosing
                End If

                AddHandler _NotifyIcon.MouseDoubleClick, AddressOf NotifyIcon_MouseDoubleClick
                _NotifyIcon.Visible = True
            End If
        End Sub

        Private _关闭窗体最小化托盘 As Boolean = True
        Public Property 关闭窗体最小化托盘() As Boolean
            Get
                Return _关闭窗体最小化托盘
            End Get
            Set(ByVal value As Boolean)
                _关闭窗体最小化托盘 = value
            End Set
        End Property

        Private _显示在任务栏 As Boolean = True
        Public Property 显示在任务栏() As Boolean
            Get
                Return _显示在任务栏
            End Get
            Set(ByVal value As Boolean)
                _显示在任务栏 = value
            End Set
        End Property

        Private _双击隐藏托盘 As Boolean = False
        Public Property 双击隐藏托盘() As Boolean
            Get
                Return _双击隐藏托盘
            End Get
            Set(ByVal value As Boolean)
                _双击隐藏托盘 = value
            End Set
        End Property
#End Region

#Region "方法"

#Region "托盘的双击事件"

        Private Sub NotifyIcon_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

            If 主窗体 IsNot Nothing Then
                If 主窗体.Visible Then
                    主窗体.Visible = False
                    主窗体.ShowInTaskbar = False
                Else
                    主窗体.ShowInTaskbar = 显示在任务栏
                    主窗体.Visible = True
                    主窗体.BringToFront()
                End If

                If 双击隐藏托盘 Then
                    Me._NotifyIcon.Visible = False
                End If
            End If

        End Sub
#End Region

#Region "托盘的退出菜单"
        Private Sub 退出ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出ToolStripMenuItem.Click

            '关闭窗口
            Dim e2 As New System.ComponentModel.CancelEventArgs
            Application.Exit(e2)

            If e2.Cancel Then
                '强制结束进程
                For Each po As Process In Process.GetProcesses
                    Try
                        If po.ProcessName = Process.GetCurrentProcess().ProcessName Then
                            po.Kill()
                        End If
                    Catch ex As Exception

                    End Try
                Next
                '强制结束进程
            End If
        End Sub
#End Region

#Region "托盘的显示菜单"
        Private Sub 显示ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 显示ToolStripMenuItem.Click
            If 主窗体 IsNot Nothing Then
                If 主窗体.Visible Then
                    主窗体.Visible = False
                    主窗体.ShowInTaskbar = False
                Else
                    主窗体.Visible = True
                    主窗体.ShowInTaskbar = 显示在任务栏
                End If
            End If
        End Sub
#End Region

#Region "初始化菜单"
        Private WithEvents 显示ToolStripMenuItem As New System.Windows.Forms.ToolStripMenuItem
        Private WithEvents 退出ToolStripMenuItem As New System.Windows.Forms.ToolStripMenuItem
        Private Sub Init菜单()
            Me.显示ToolStripMenuItem.Image = My.Resources.Resources.显示.ToBitmap
            Me.显示ToolStripMenuItem.Name = "显示ToolStripMenuItem"
            Me.显示ToolStripMenuItem.Size = New System.Drawing.Size(112, 22)
            Me.显示ToolStripMenuItem.Text = "显示"
            AddHandler Me.显示ToolStripMenuItem.Click, AddressOf 显示ToolStripMenuItem_Click

            Me.退出ToolStripMenuItem.Image = My.Resources.Resources.退出.ToBitmap
            Me.退出ToolStripMenuItem.Name = "退出ToolStripMenuItem"
            Me.退出ToolStripMenuItem.Size = New System.Drawing.Size(112, 22)
            Me.退出ToolStripMenuItem.Text = "退出"
            AddHandler Me.退出ToolStripMenuItem.Click, AddressOf 退出ToolStripMenuItem_Click

            Me._contextMenu.Items.Add(显示ToolStripMenuItem)
            Me._contextMenu.Items.Add(退出ToolStripMenuItem)
        End Sub
#End Region

#Region "窗体的准备关闭事件"
        Private Sub MainSecond_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)

            If Not 关闭窗体最小化托盘 OrElse (e.CloseReason <> CloseReason.UserClosing) Then
                Exit Sub
            End If

            ShowText("程序在任务栏运行中")
            主窗体.Hide()
            Me._NotifyIcon.Visible = True
            e.Cancel = True

        End Sub
#End Region

#Region "托盘文字"
        Public Sub ShowText(ByVal strContent As String)
            Me._NotifyIcon.BalloonTipIcon = ToolTipIcon.Warning
            Me._NotifyIcon.BalloonTipText = strContent
            Me._NotifyIcon.BalloonTipTitle = 主窗体.Text
            Me._NotifyIcon.ShowBalloonTip(600)
        End Sub
#End Region

#End Region

#Region "事件"

#End Region

    End Class
End Namespace

